#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;

void test0(){
    int arr[5] = {1,2,3,4,5};

    //<>中写vector要存放的元素的类型
    //模板参数（类型参数）
    //vector是类模板实现的
    //vector代表一类类
    //vector<int>可以代表一个特定的类
    vector<int> nums;
    cout << "is nums empty?" << nums.empty() << endl;

    //10代表存放元素的个数
    vector<long> nums2(10);

    for(auto & ele : nums2){
        cout << ele << " ";
    }
    cout << endl;

    //存放5个8
    vector<int> nums3(5,8);
    for(auto & ele : nums3){
        cout << ele << " ";
    }
    cout << endl;

    //大括号形式
    vector<int> nums4{1,2,3,4,5,6,7};
    for(auto & ele : nums4){
        cout << ele << " ";
    }
    cout << endl;


    //迭代器形式创建vector
    vector<int> nums5(nums4.begin(),nums4.end() - 3);
    for(auto & ele : nums5){
        cout << ele << " ";
    }
    cout << endl;


    //这样就没有复制arr的最后一个元素
    //左闭右开原则
    vector<int> nums6(arr,arr + 4);
    for(auto & ele : nums6){
        cout << ele << " ";
    }
    cout << endl;
}

//vectro的常用操作
void test1(){
    vector<int> nums{1,2,3,4,5};
    cout << "nums'size:" << nums.size() << endl;
    cout << "nums'capacity:" << nums.capacity() << endl;

    cout << endl;
    nums.push_back(1);
    for(auto & ele : nums){
        cout << ele << " ";
    }
    cout << endl;
    cout << "nums'size:" << nums.size() << endl;
    cout << "nums'capacity:" << nums.capacity() << endl;

    cout << endl;
    nums.pop_back();
    for(auto & ele : nums){
        cout << ele << " ";
    }
    cout << endl;
    cout << "nums'size:" << nums.size() << endl;
    cout << "nums'capacity:" << nums.capacity() << endl;

    cout << endl;
    nums.shrink_to_fit();
    cout << "nums'size:" << nums.size() << endl;
    cout << "nums'capacity:" << nums.capacity() << endl;

    nums.clear();
    cout << "nums'size:" << nums.size() << endl;
    cout << "nums'capacity:" << nums.capacity() << endl;
}


void test2(){
    vector<int> nums{1,2,3,4,5};
    cout << &nums << endl;
    cout << &nums[0] << endl;
    cout << &nums[1] << endl;

    vector<int>::iterator it = nums.begin();
    cout << &*nums.begin() << endl;

    //vector的元素一定在堆上
    //vector对象可以创建在栈上，也可以创建在堆上
    vector<int> * p = new vector<int>();
    p->push_back(10);
    delete p;

}

void test3(){
    //如果一个vector要存放很多个元素
    
    //这种有参构造不仅开辟了存放元素的空间
    //而且对所有元素进行了初始化
    vector<int> nums(10);
    cout << "nums'size:" << nums.size() << endl;
    cout << "nums'capacity:" << nums.capacity() << endl;

    vector<int> nums2;
    nums2.reserve(10);//开辟空间，但是没有存放元素
    cout << "nums2'size:" << nums2.size() << endl;
    cout << "nums2'capacity:" << nums2.capacity() << endl;

}


int main(void){
    test3();
    return 0;
}
